# Who Will Defend Democracy? Evaluating Tradeoffs in Candidate Support Among Partisan Donors and Voters
# Step 4: Predicted support probabilities 
# Last updated: June 19, 2020

# Initial settings --------------------------------------------------------

library(tidyverse)
library(estimatr)

# Load data (donors) ---------------------------------------------------------------

load("temp/data_for_analysis_donors.RData")

# Re-specify baselines (donors) ----------------------------------------------------

df_republican_donors <- df_cleaned_donors %>% filter(partisanship_dum == 1)

df_republican_donors$Gender <- relevel(df_republican_donors$Gender, ref = "Male")
df_republican_donors$Race <- relevel(df_republican_donors$Race, ref = "White")
df_republican_donors$Partisanship <- relevel(df_republican_donors$Partisanship, ref = "Republican")
df_republican_donors$Discrimination <- relevel(df_republican_donors$Discrimination, ref = "Not a big problem")
df_republican_donors$Taxes <- relevel(df_republican_donors$Taxes, ref = "Less progressive")
df_republican_donors$Voting <- relevel(df_republican_donors$Voting, ref = "Supports voter ID laws")
df_republican_donors$Compromise <- relevel(df_republican_donors$Compromise, ref = "Supports compromise")
df_republican_donors$Courts <- relevel(df_republican_donors$Courts, ref = "Obey courts")

df_democrat_donors <- df_cleaned_donors %>% filter(partisanship_dum == 2)

df_democrat_donors$Gender <- relevel(df_democrat_donors$Gender, ref = "Male")
df_democrat_donors$Race <- relevel(df_democrat_donors$Race, ref = "White")
df_democrat_donors$Partisanship <- relevel(df_democrat_donors$Partisanship, ref = "Democrat")
df_democrat_donors$Discrimination <- relevel(df_democrat_donors$Discrimination, ref = "Prevent discrimination")
df_democrat_donors$Taxes <- relevel(df_democrat_donors$Taxes, ref = "More progressive")
df_democrat_donors$Voting <- relevel(df_democrat_donors$Voting, ref = "Opposes voter ID laws")
df_democrat_donors$Compromise <- relevel(df_democrat_donors$Compromise, ref = "Supports compromise")
df_democrat_donors$Courts <- relevel(df_democrat_donors$Courts, ref = "Obey courts")

# Specify models (donors) ----------------------------------------------------------

model_donors<- formula(selected ~ 
                   `Gender` +
                   `Race` +
                   `Partisanship` +
                   `Taxes` +
                   `Discrimination` +
                   `Courts` +
                   `Investigations` + 
                   `Compromise` +
                   `Voting`)

lm_robust(model_donors, data = df_republican_donors, clusters = id) %>% summary()
republican_donors <- lm_robust(model_donors, data = df_republican_donors, clusters = id) 

lm_robust(model_donors, data = df_democrat_donors, clusters = id) %>% summary()
democrat_donors <- lm_robust(model_donors, data = df_democrat_donors, clusters = id) 

# Calculate predicted support probabilities (donors) -----------------------------------------

# Typical Republican candidate with no democratic violations (among Republican donors)

predict(republican_donors, data_frame(Gender = c("Male"),
                                      Race = c("White"),
                                      Partisanship = c("Republican"),
                                      Discrimination = c("Not a big problem"),
                                      Taxes = c("Less progressive"),
                                      Voting = c("Supports voter ID laws"),
                                      Courts = c("Obey courts"),
                                      Compromise = c("Supports compromise"),
                                      Investigations = c("Independent")))

# Typical Republican candidate with one democratic violation (among Republican donors)

predict(republican_donors, data_frame(Gender = c("Male"),
                                      Race = c("White"),
                                      Partisanship = c("Republican"),
                                      Discrimination = c("Not a big problem"),
                                      Taxes = c("Less progressive"),
                                      Voting = c("Supports voter ID laws"),
                                      Courts = c("Obey courts"),
                                      Compromise = c("Stand up to other party"),
                                      Investigations = c("Independent")))

predict(republican_donors, data_frame(Gender = c("Male"),
                                      Race = c("White"),
                                      Partisanship = c("Republican"),
                                      Discrimination = c("Not a big problem"),
                                      Taxes = c("Less progressive"),
                                      Voting = c("Supports voter ID laws"),
                                      Courts = c("Disregard politicized decisions"),
                                      Compromise = c("Supports compromise"),
                                      Investigations = c("Independent")))

predict(republican_donors, data_frame(Gender = c("Male"),
                                      Race = c("White"),
                                      Partisanship = c("Republican"),
                                      Discrimination = c("Not a big problem"),
                                      Taxes = c("Less progressive"),
                                      Voting = c("Supports voter ID laws"),
                                      Courts = c("Obey courts"),
                                      Compromise = c("Supports compromise"),
                                      Investigations = c("Partisan involvement")))


# Typical Republican candidate with two democratic violations (among Republican donors)

predict(republican_donors, data_frame(Gender = c("Male"),
                                      Race = c("White"),
                                      Partisanship = c("Republican"),
                                      Discrimination = c("Not a big problem"),
                                      Taxes = c("Less progressive"),
                                      Voting = c("Supports voter ID laws"),
                                      Courts = c("Disregard politicized decisions"),
                                      Compromise = c("Stand up to other party"),
                                      Investigations = c("Independent")))

predict(republican_donors, data_frame(Gender = c("Male"),
                                      Race = c("White"),
                                      Partisanship = c("Republican"),
                                      Discrimination = c("Not a big problem"),
                                      Taxes = c("Less progressive"),
                                      Voting = c("Supports voter ID laws"),
                                      Courts = c("Obey courts"),
                                      Compromise = c("Stand up to other party"),
                                      Investigations = c("Partisan involvement")))

predict(republican_donors, data_frame(Gender = c("Male"),
                                      Race = c("White"),
                                      Partisanship = c("Republican"),
                                      Discrimination = c("Not a big problem"),
                                      Taxes = c("Less progressive"),
                                      Voting = c("Supports voter ID laws"),
                                      Courts = c("Disregard politicized decisions"),
                                      Compromise = c("Supports compromise"),
                                      Investigations = c("Partisan involvement")))

# Typical Republican candidate with three democratic violations (among Republican donors)

predict(republican_donors, data_frame(Gender = c("Male"),
                                      Race = c("White"),
                                      Partisanship = c("Republican"),
                                      Discrimination = c("Not a big problem"),
                                      Taxes = c("Less progressive"),
                                      Voting = c("Supports voter ID laws"),
                                      Courts = c("Disregard politicized decisions"),
                                      Compromise = c("Stand up to other party"),
                                      Investigations = c("Partisan involvement")))

# Typical Democratic candidate with no democratic violations (among Democratic donors)

predict(democrat_donors, data_frame(Gender = c("Male"),
                                    Race = c("White"),
                                    Partisanship = c("Democrat"),
                                    Discrimination = c("Prevent discrimination"),
                                    Taxes = c("More progressive"),
                                    Voting = c("Opposes voter ID laws"),
                                    Courts = c("Obey courts"),
                                    Compromise = c("Supports compromise"),
                                    Investigations = c("Independent")))

# Typical Democratic candidate with one democratic violation (among R)

predict(democrat_donors, data_frame(Gender = c("Male"),
                                    Race = c("White"),
                                    Partisanship = c("Democrat"),
                                    Discrimination = c("Prevent discrimination"),
                                    Taxes = c("More progressive"),
                                    Voting = c("Opposes voter ID laws"),
                                    Courts = c("Obey courts"),
                                    Compromise = c("Stand up to other party"),
                                    Investigations = c("Independent")))

predict(democrat_donors, data_frame(Gender = c("Male"),
                                    Race = c("White"),
                                    Partisanship = c("Democrat"),
                                    Discrimination = c("Prevent discrimination"),
                                    Taxes = c("More progressive"),
                                    Voting = c("Opposes voter ID laws"),
                                    Courts = c("Disregard politicized decisions"),
                                    Compromise = c("Supports compromise"),
                                    Investigations = c("Independent")))

predict(democrat_donors, data_frame(Gender = c("Male"),
                                    Race = c("White"),
                                    Partisanship = c("Democrat"),
                                    Discrimination = c("Prevent discrimination"),
                                    Taxes = c("More progressive"),
                                    Voting = c("Opposes voter ID laws"),
                                    Courts = c("Obey courts"),
                                    Compromise = c("Supports compromise"),
                                    Investigations = c("Partisan involvement")))

# Typical Democratic candidate with two democratic violations (among Democratic donors)

predict(democrat_donors, data_frame(Gender = c("Male"),
                                    Race = c("White"),
                                    Partisanship = c("Democrat"),
                                    Discrimination = c("Prevent discrimination"),
                                    Taxes = c("More progressive"),
                                    Voting = c("Opposes voter ID laws"),
                                    Courts = c("Disregard politicized decisions"),
                                    Compromise = c("Stand up to other party"),
                                    Investigations = c("Independent")))

predict(democrat_donors, data_frame(Gender = c("Male"),
                                    Race = c("White"),
                                    Partisanship = c("Democrat"),
                                    Discrimination = c("Prevent discrimination"),
                                    Taxes = c("More progressive"),
                                    Voting = c("Opposes voter ID laws"),
                                    Courts = c("Obey courts"),
                                    Compromise = c("Stand up to other party"),
                                    Investigations = c("Partisan involvement")))

predict(democrat_donors, data_frame(Gender = c("Male"),
                                    Race = c("White"),
                                    Partisanship = c("Democrat"),
                                    Discrimination = c("Prevent discrimination"),
                                    Taxes = c("More progressive"),
                                    Voting = c("Opposes voter ID laws"),
                                    Courts = c("Disregard politicized decisions"),
                                    Compromise = c("Supports compromise"),
                                    Investigations = c("Partisan involvement")))

# Typical Democratic candidate with three democratic violations (among Democratic donors)

predict(democrat_donors, data_frame(Gender = c("Male"),
                                    Race = c("White"),
                                    Partisanship = c("Democrat"),
                                    Discrimination = c("Prevent discrimination"),
                                    Taxes = c("More progressive"),
                                    Voting = c("Opposes voter ID laws"),
                                    Courts = c("Disregard politicized decisions"),
                                    Compromise = c("Stand up to other party"),
                                    Investigations = c("Partisan involvement")))

# Load data (public) ---------------------------------------------------------------

load("temp/data_for_analysis_public.RData")

# Re-specify baselines (public) ----------------------------------------------------

df_republican_public <- df_cleaned_public %>% filter(partisanship_dum_7 == 1)

df_republican_public$Gender <- relevel(df_republican_public$Gender, ref = "Male")
df_republican_public$Race <- relevel(df_republican_public$Race, ref = "White")
df_republican_public$Partisanship <- relevel(df_republican_public$Partisanship, ref = "Republican")
df_republican_public$Discrimination <- relevel(df_republican_public$Discrimination, ref = "Not a big problem")
df_republican_public$Taxes <- relevel(df_republican_public$Taxes, ref = "Less progressive")
df_republican_public$Voting <- relevel(df_republican_public$Voting, ref = "Supports voter ID laws")
df_republican_public$Compromise <- relevel(df_republican_public$Compromise, ref = "Supports compromise")
df_republican_public$Courts <- relevel(df_republican_public$Courts, ref = "Obey courts")

df_democrat_public <- df_cleaned_public %>% filter(partisanship_dum_7 == 2)

df_democrat_public$Gender <- relevel(df_democrat_public$Gender, ref = "Male")
df_democrat_public$Race <- relevel(df_democrat_public$Race, ref = "White")
df_democrat_public$Partisanship <- relevel(df_democrat_public$Partisanship, ref = "Democrat")
df_democrat_public$Discrimination <- relevel(df_democrat_public$Discrimination, ref = "Prevent discrimination")
df_democrat_public$Taxes <- relevel(df_democrat_public$Taxes, ref = "More progressive")
df_democrat_public$Voting <- relevel(df_democrat_public$Voting, ref = "Opposes voter ID laws")
df_democrat_public$Compromise <- relevel(df_democrat_public$Compromise, ref = "Supports compromise")
df_democrat_public$Courts <- relevel(df_democrat_public$Courts, ref = "Obey courts")

# Specify models (public) ----------------------------------------------------------

model_public <- formula(selected ~ 
                         `Gender` +
                         `Race` +
                         `Partisanship` +
                         `Taxes` +
                         `Discrimination` +
                         `Courts` +
                         `Investigations` + 
                         `Compromise` +
                         `Voting`)

lm_robust(model_public, data = df_republican_public, clusters = id) %>% summary()
republican_public <- lm_robust(model_public, data = df_republican_public, clusters = id) 

lm_robust(model_public, data = df_democrat_public, clusters = id) %>% summary()
democrat_public <- lm_robust(model_public, data = df_democrat_public, clusters = id) 

# Calculate predicted support probabilities (public) -----------------------------------------

predict(republican_public, data_frame(Gender = c("Male"),
                                     Race = c("White"),
                                     Partisanship = c("Republican"),
                                     Discrimination = c("Not a big problem"),
                                     Taxes = c("Less progressive"),
                                     Voting = c("Supports voter ID laws"),
                                     Courts = c("Obey courts"),
                                     Compromise = c("Supports compromise"),
                                     Investigations = c("Independent")))

# Typical Republican candidate with one democratic violation (among Republican public)

predict(republican_public, data_frame(Gender = c("Male"),
                                     Race = c("White"),
                                     Partisanship = c("Republican"),
                                     Discrimination = c("Not a big problem"),
                                     Taxes = c("Less progressive"),
                                     Voting = c("Supports voter ID laws"),
                                     Courts = c("Obey courts"),
                                     Compromise = c("Stand up to other party"),
                                     Investigations = c("Independent")))

predict(republican_public, data_frame(Gender = c("Male"),
                                     Race = c("White"),
                                     Partisanship = c("Republican"),
                                     Discrimination = c("Not a big problem"),
                                     Taxes = c("Less progressive"),
                                     Voting = c("Supports voter ID laws"),
                                     Courts = c("Disregard politicized decisions"),
                                     Compromise = c("Supports compromise"),
                                     Investigations = c("Independent")))

predict(republican_public, data_frame(Gender = c("Male"),
                                     Race = c("White"),
                                     Partisanship = c("Republican"),
                                     Discrimination = c("Not a big problem"),
                                     Taxes = c("Less progressive"),
                                     Voting = c("Supports voter ID laws"),
                                     Courts = c("Obey courts"),
                                     Compromise = c("Supports compromise"),
                                     Investigations = c("Partisan involvement")))


# Typical Republican candidate with two democratic violations (among Republican public)

predict(republican_public, data_frame(Gender = c("Male"),
                                     Race = c("White"),
                                     Partisanship = c("Republican"),
                                     Discrimination = c("Not a big problem"),
                                     Taxes = c("Less progressive"),
                                     Voting = c("Supports voter ID laws"),
                                     Courts = c("Disregard politicized decisions"),
                                     Compromise = c("Stand up to other party"),
                                     Investigations = c("Independent")))

predict(republican_public, data_frame(Gender = c("Male"),
                                     Race = c("White"),
                                     Partisanship = c("Republican"),
                                     Discrimination = c("Not a big problem"),
                                     Taxes = c("Less progressive"),
                                     Voting = c("Supports voter ID laws"),
                                     Courts = c("Obey courts"),
                                     Compromise = c("Stand up to other party"),
                                     Investigations = c("Partisan involvement")))

predict(republican_public, data_frame(Gender = c("Male"),
                                     Race = c("White"),
                                     Partisanship = c("Republican"),
                                     Discrimination = c("Not a big problem"),
                                     Taxes = c("Less progressive"),
                                     Voting = c("Supports voter ID laws"),
                                     Courts = c("Disregard politicized decisions"),
                                     Compromise = c("Supports compromise"),
                                     Investigations = c("Partisan involvement")))

# Typical Republican candidate with three democratic violations (among Republican public)

predict(republican_public, data_frame(Gender = c("Male"),
                                     Race = c("White"),
                                     Partisanship = c("Republican"),
                                     Discrimination = c("Not a big problem"),
                                     Taxes = c("Less progressive"),
                                     Voting = c("Supports voter ID laws"),
                                     Courts = c("Disregard politicized decisions"),
                                     Compromise = c("Stand up to other party"),
                                     Investigations = c("Partisan involvement")))

# Typical Democratic candidate with no democratic violations (among Democratic public)

predict(democrat_public, data_frame(Gender = c("Male"),
                                    Race = c("White"),
                                    Partisanship = c("Democrat"),
                                    Discrimination = c("Prevent discrimination"),
                                    Taxes = c("More progressive"),
                                    Voting = c("Opposes voter ID laws"),
                                    Courts = c("Obey courts"),
                                    Compromise = c("Supports compromise"),
                                    Investigations = c("Independent")))

# Typical Democratic candidate with one democratic violation (among Democratic public)

predict(democrat_public, data_frame(Gender = c("Male"),
                                    Race = c("White"),
                                    Partisanship = c("Democrat"),
                                    Discrimination = c("Prevent discrimination"),
                                    Taxes = c("More progressive"),
                                    Voting = c("Opposes voter ID laws"),
                                    Courts = c("Obey courts"),
                                    Compromise = c("Stand up to other party"),
                                    Investigations = c("Independent")))

predict(democrat_public, data_frame(Gender = c("Male"),
                                    Race = c("White"),
                                    Partisanship = c("Democrat"),
                                    Discrimination = c("Prevent discrimination"),
                                    Taxes = c("More progressive"),
                                    Voting = c("Opposes voter ID laws"),
                                    Courts = c("Disregard politicized decisions"),
                                    Compromise = c("Supports compromise"),
                                    Investigations = c("Independent")))

predict(democrat_public, data_frame(Gender = c("Male"),
                                    Race = c("White"),
                                    Partisanship = c("Democrat"),
                                    Discrimination = c("Prevent discrimination"),
                                    Taxes = c("More progressive"),
                                    Voting = c("Opposes voter ID laws"),
                                    Courts = c("Obey courts"),
                                    Compromise = c("Supports compromise"),
                                    Investigations = c("Partisan involvement")))

# Typical Democratic candidate with two democratic violations (among Democratic public)

predict(democrat_public, data_frame(Gender = c("Male"),
                                    Race = c("White"),
                                    Partisanship = c("Democrat"),
                                    Discrimination = c("Prevent discrimination"),
                                    Taxes = c("More progressive"),
                                    Voting = c("Opposes voter ID laws"),
                                    Courts = c("Disregard politicized decisions"),
                                    Compromise = c("Stand up to other party"),
                                    Investigations = c("Independent")))

predict(democrat_public, data_frame(Gender = c("Male"),
                                    Race = c("White"),
                                    Partisanship = c("Democrat"),
                                    Discrimination = c("Prevent discrimination"),
                                    Taxes = c("More progressive"),
                                    Voting = c("Opposes voter ID laws"),
                                    Courts = c("Obey courts"),
                                    Compromise = c("Stand up to other party"),
                                    Investigations = c("Partisan involvement")))

predict(democrat_public, data_frame(Gender = c("Male"),
                                    Race = c("White"),
                                    Partisanship = c("Democrat"),
                                    Discrimination = c("Prevent discrimination"),
                                    Taxes = c("More progressive"),
                                    Voting = c("Opposes voter ID laws"),
                                    Courts = c("Disregard politicized decisions"),
                                    Compromise = c("Supports compromise"),
                                    Investigations = c("Partisan involvement")))

# Typical Democratic candidate with three democratic violations (among Democratic public)

predict(democrat_public, data_frame(Gender = c("Male"),
                                    Race = c("White"),
                                    Partisanship = c("Democrat"),
                                    Discrimination = c("Prevent discrimination"),
                                    Taxes = c("More progressive"),
                                    Voting = c("Opposes voter ID laws"),
                                    Courts = c("Disregard politicized decisions"),
                                    Compromise = c("Stand up to other party"),
                                    Investigations = c("Partisan involvement")))
